<template>
  <page-header-wrapper>
    <a-row>
      <a-col span="24">
        <a-card :loading="loading" title="密码修改">
          <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules" v-bind="layout">
            <a-form-model-item has-feedback label="新密码" prop="pass">
              <a-input v-model="ruleForm.pass" type="password" autocomplete="off" />
            </a-form-model-item>
            <a-form-model-item has-feedback label="请再次输入密码" prop="checkPass">
              <a-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
            </a-form-model-item>
            <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
              <a-button type="primary" @click="submitForm('ruleForm')">
                提交
              </a-button>
              <a-button style="margin-left: 10px" @click="resetForm('ruleForm')">
                重置
              </a-button>
            </a-form-model-item>
          </a-form-model>
        </a-card>
        <!-- <a-button style="marginTop: 16px" @click="handleClick">
          Toggle loading
        </a-button> -->
      </a-col>
    </a-row>
  </page-header-wrapper>
</template>

<script>
import { passwordEdit } from '@/api/login'
import md5 from 'md5'
export default {
  data () {
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码必须大于6位'))
      }
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入的密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        pass: '',
        checkPass: ''
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'change' }],
        checkPass: [{ validator: validatePass2, trigger: 'change' }]
      },
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 14 }
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var fd = new FormData()
          fd.append('password', md5(this.ruleForm.checkPass))
          passwordEdit(fd).then(res => {
            if (res.code === 200) {
              this.$notification.success({
                title: '成功',
                message: '您已成功修改密码'
              })
              this.$router.push('/program/programDiscuss')
            }
          }).catch(err => {
            this.$notification.error({
              title: err.code,
              message: err.message
            })
          })
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
